//
//  TweetSendImageCCell.m
//  Juxin
//
//  Created by wangwei on 15/5/29.
//  Copyright (c) 2015年 季诺. All rights reserved.
//


#import "TweetSendImageCCell.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "RACEXTScope.h"

@implementation TweetSendImageCCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)setCurTweetImg:(TweetImage *)curTweetImg{
    if (!_imgView) {
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kTweetSendImageCCell_Width, kTweetSendImageCCell_height)];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.clipsToBounds = YES;
        _imgView.layer.masksToBounds = YES;
        [self.contentView addSubview:_imgView];
    }
    
    _curTweetImg = curTweetImg;
    if (_curTweetImg) {
        if (!_deleteBtn) {
            //删除按钮
            _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(kTweetSendImageCCell_Width-20, 0, 20, 20)];
            [_deleteBtn setImage:[UIImage imageNamed:@"btn_delete_tweetimage"] forState:UIControlStateNormal];
            _deleteBtn.backgroundColor = [UIColor grayColor];
            _deleteBtn.layer.cornerRadius = CGRectGetWidth(_deleteBtn.bounds)/2;
            _deleteBtn.layer.masksToBounds = YES;
            
            [_deleteBtn addTarget:self action:@selector(deleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.contentView addSubview:_deleteBtn];
            
            if(_isDEL){
                //巡检带入 不添加删除按钮
                _deleteBtn.hidden=YES;
            }else{
                _deleteBtn.hidden = !self.isEdit;
            }
        }else{
            if(_isDEL){
                _deleteBtn.hidden=YES;
                //巡检带入 不添加删除按钮
            }else{
                _deleteBtn.hidden = !self.isEdit;
            }
        }
        
        RAC(self.imgView, image) = [RACObserve(_curTweetImg, thumbnailImage) takeUntil:self.rac_prepareForReuseSignal];
        
    }else{
        //添加图片
        _imgView.image = [UIImage imageNamed:@"addPictureBgImage"];
        
        if (_deleteBtn) {
            _deleteBtn.hidden = YES;
        }
    }
}

//设置删除按钮
-(void)setIsDEL:(BOOL)isDEL{
    _isDEL=isDEL;
    
}
- (void)deleteBtnClicked:(id)sender{
    if (_deleteTweetImageBlock) {
        _deleteTweetImageBlock(_curTweetImg);
    }
}
+(CGSize)ccellSize {
    return CGSizeMake(kTweetSendImageCCell_Width, kTweetSendImageCCell_height);
}

@end
